var components = [];

var totalKeyboardArray = [];
totalKeyboardArray[2] = ["keyboard_0", "keyboard_1", "keyboard_2", "keyboard_3", "keyboard_4", "keyboard_5", "keyboard_6", "keyboard_7", "keyboard_8", "keyboard_9", "keyboard_dot", "keyboard_at", "keyboard_sharp"];
totalKeyboardArray[0] = ["keyboard_a", "keyboard_b", "keyboard_c", "keyboard_d", "keyboard_e", "keyboard_f", "keyboard_g", "keyboard_h", "keyboard_i", "keyboard_j", "keyboard_k", "keyboard_l", "keyboard_m"];
totalKeyboardArray[1] = ["keyboard_n", "keyboard_o", "keyboard_p", "keyboard_q", "keyboard_r", "keyboard_s", "keyboard_t", "keyboard_u", "keyboard_v", "keyboard_w", "keyboard_x", "keyboard_y", "keyboard_z"];

var keyboardArray = totalKeyboardArray;

var operateArray = [];
operateArray[0] = ["keyboard_switch", "keyboard_zh", "keyboard_dele", "keyboard_close"];


var selectWordArray = [];
selectWordArray[0] = [];

var pageArray = [];
pageArray[0] = ["button-next", "button-pre"];

var index_x = 0;
var index_y = 0;
var inputFlag = false;
components = keyboardArray;

/**键盘字母输入处理*/
var letters = "";
//是否中途进行过删除操作
var isDelete = 0;
//是否是中文输入，等于1表示是中文输入
var isChinese = 0;
//是否是小写输入，等于1表示是小写输
var isLowerCase = 1;
//保存打出来的词数组，用于分页
var pagesArray = [];
// 字符容器，用于信息交互
var charContainer = '';
// 指示中文搜索字母是否为空
var chineseSearch = -1;

/**按下字母键盘处理*/
function click_keyboard(e, element, action) {
    if (action == "leave") {
        $("#" + element).attr("class", "span");
    } else if (action == "focus") {
        $("#" + element).attr("class", "span_now");
    }
    else if (action == "click" && (e == '' || e && e.keyCode == 13)) {
        pagesArray = [];//每次按下字母时都需要先清空原来的数组
        if (letters != "") {
            if (isDelete == 1) {
                letters = $("#input_letters").text() + $("#" + element).text();
            } else {
                letters = letters + $("#" + element).text();
            }
        } else {
            letters = $("#" + element).text();
        }
        $("#input_letters").text(letters);
        if (isChinese == 1) {//中文输入时才需要调字典库
            if (jsondata.hasOwnProperty(letters)) {
                handle_input(letters);
            } else {
                clean_elements();
            }
        } else {//英文输入时直接把字符赋值到输入框
            select_EN_word(letters);
        }
    }
}


var inputspace = "";
/**键盘操作输入处理*/
function operate_keyboard(e, element, action) {
    if (action == "leave") {
        $("#" + element).attr("class", element);
    } else if (action == "focus") {
        $("#" + element).attr("class", element + "_over");
    }
    else if (action == "click" && (e == '' || e && e.keyCode == 13)) {
        if (element == "keyboard_dele") {//删除输入字母操作
            pagesArray = [];//每次按下字母时都需要先清空原来的数组
            isDelete = 1;
            var temp = $("#input_letters").text();
            var length = temp.length;
            length--;
            var innerletters = "";
            if (length > 0) {//长度大于0时，才取词
                chineseSearch = 1;
                innerletters = temp.substring(0, length);
                if (isChinese == 1) {
                    if (jsondata.hasOwnProperty(innerletters)) {
                        handle_input(innerletters);
                    } else {
                        clean_elements();
                    }
                } else {//英文输入时直接把字符赋值到输入框
                    select_EN_word(letters);
                }
            } else {
                chineseSearch--;
                clean_elements();
            }
            $("#input_letters").text(innerletters);
        }
        if (element == "keyboard_close") {//关闭软键盘
            $("#virtualKeyboard").attr("class", "keyboard_hide");
            recover_old_index();
            inputFlag = false;
            charContainer = '';
            letters = '';
            isChinese = 0;
            chineseSearch = -1;
        }
        if (element == "keyboard_zh") {//中英文切换
            //中文下清除所有的输入，包括已保存的letters
            clean_elements();
            if ($("#keyboard_zh").text().match("中/英")) {
                $("#keyboard_zh").text("英/中");
                $("#virtualKeyboard").attr("class", "virtualKeyboard_en");
                $("#words_input").attr("class", "keyboard_hide");
                $("#words_list").attr("class", "keyboard_hide");
                $("#button_page").attr("class", "keyboard_hide");
                isChinese = 0;
            } else {
                $("#keyboard_zh").text("中/英");
                $("#virtualKeyboard").attr("class", "virtualKeyboard");
                $("#words_input").attr("class", "words_input");
                $("#words_list").attr("class", "words_list");
                $("#button_page").attr("class", "button_page");
                isChinese = 1;
                $("#input_letters").empty();
                letters = "";
                // 换成小写
                $("#keyboard_switch").text("A/a");
                isLowerCase = 1;
                change_letterUppperOrLower(isLowerCase);
            }
        }
        if (element == "keyboard_switch") {//大小写切换
            //中文下清除所有的输入，包括已保存的letters
            clean_elements();
            if (isChinese == 0) {//必须在英文下才可以切换大小写
                if ($("#keyboard_switch").text().match("A/a")) {
                    $("#keyboard_switch").text("a/A");
                    isLowerCase = 0;
                    change_letterUppperOrLower(isLowerCase);
                } else {
                    $("#input_letters").empty();
                    letters = "";
                    $("#keyboard_switch").text("A/a");
                    isLowerCase = 1;
                    change_letterUppperOrLower(isLowerCase);
                }
            }
        }
    }
}


/**处理输入字符时，查询到的字符*/
function handle_input(innerletters) {
    clean_elements();
    selectWordArray[0] = [];//每次需要先清空数组的值，不然无法重新赋值
    var tempWords = jsondata[innerletters];
    if (tempWords.length > 10) {//需要分页
        var temp = "";
        var numstr = tempWords.length / 10;
        for (var i = 0; i < numstr; i++) {
            if (tempWords.length >= 10) {
                temp = tempWords.substring(0, 10);
                tempWords = tempWords.substring(10);
            } else {
                temp = tempWords;
            }
            pagesArray[i] = temp;
        }
    }
    if (pagesArray.length == 0) {//如果没有翻页就隐藏翻页按钮
        initWordElements(tempWords.length);
        for (var i = 0; i < tempWords.length; i++) {
            selectWordArray[0][i] = "select_words-" + i;
            $("#" + selectWordArray[0][i]).text(i + "." + tempWords.substr(i, 1));
        }
    } else {
        initWordElements(10);
        for (var i = 0; i < 10; i++) {
            selectWordArray[0][i] = "select_words-" + i;
            $("#" + selectWordArray[0][i]).text(i + "." + pagesArray[0].substr(i, 1));
        }
    }
}


/**大小写字母切换
 * 遍历字母下标ID,进行大小切换
 */
function change_letterUppperOrLower(isLowerCase) {
    for (var i = 0; i < keyboardArray.length; i++) {
        var kbArray = new Array();
        kbArray = keyboardArray[i];
        for (var j = 0; j < kbArray.length; j++) {
            if (isLowerCase == 1) {
                $("#" + kbArray[j]).text($("#" + kbArray[j]).text().toLowerCase());
            } else {
                $("#" + kbArray[j]).text($("#" + kbArray[j]).text().toUpperCase());
            }
        }
    }
}

var pages_total = 0;
var pages_index = 0;


/**翻页按钮*/
function switch_page_btn(e, element, action) {
    if (element == "button-next" || element == "button-pre") {//翻页事件
        if (action == "leave") {
            $("#" + element).attr("class", element);
        } else if (action == "focus") {
            $("#" + element).attr("class", element + "_over");
        } else if (action == "click" && (e == '' || e && e.keyCode == 13)) {
            pages_total = pagesArray.length;
            if (element == "button-next") {//下一页处理
                if (pagesArray.length > 1) {// 大于1才能进行翻页
                    pages_index++;
                    if (pages_index > pages_total - 1) {
                        pages_index = pages_total - 1;
                    }
                    var len = pagesArray[pages_index].length;
                    clean_elements();
                    selectWordArray[0] = [];//每次需要先清空数组的值，不然无法重新赋值
                    initWordElements(len);
                    for (var i = 0; i < len; i++) {
                        selectWordArray[0][i] = "select_words-" + i;
                        $("#" + selectWordArray[0][i]).text(i + "." + pagesArray[pages_index].substr(i, 1) + "  ");
                    }
                }
            } else {//上一页处理
                if (pagesArray.length > 1 && pages_index >= 1) {// 大于1才能进行翻页
                    pages_index--;
                    if (pages_index < 0) {
                        pages_index = 0;
                    }
                    var len = pagesArray[pages_index].length;
                    clean_elements();
                    selectWordArray[0] = [];//每次需要先清空数组的值，不然无法重新赋值
                    initWordElements(len);
                    for (var i = 0; i < len; i++) {
                        selectWordArray[0][i] = "select_words-" + i;
                        $("#" + selectWordArray[0][i]).text(i + "." + pagesArray[pages_index].substr(i, 1) + "  ");
                    }
                }
            }
        }
    }
}

/**根据长度初始化选词的span*/
function initWordElements(length) {
    if (length > 0) {
        var elements = "";
        for (var i = 0; i < length; i++) {
            elements += "<span id='select_words-" + i + "' class='select_words' tabindex='0' style='outline:none;' onclick=\"select_word('','select_words-" + i + "','click')\"  onmouseover=\"select_word('','select_words-" + i + "','focus')\" onmouseleave=\"select_word('','select_words-" + i + "','leave')\"" +
                "onkeydown=\"select_word(event,'select_words-" + i + "','click')\" onfocus=\"select_word('','select_words-" + i + "','focus')\" onblur=\"select_word('','select_words-" + i + "','leave')\"></span>";
        }
        $("#words_list").html(elements);
    }

}

/**清空节点的内容*/
function clean_elements() {
    for (var i = 0; i < selectWordArray.length; i++) {
        var temElements = new Array();
        temElements = selectWordArray[i];
        for (var j = 0; j < temElements.length; j++) {
            $("#" + temElements[j]).empty();
        }
    }
}


/**英文输入下时把打印出来的字赋值到对应的文本框中
 * 由子类重写
 * */
function select_EN_word(value) {
}